#include <cstdlib>

#include "prng.h"

int16_t IPrng::getRand()
{
    if (mModulo <= 0)
        return -1;
    if (mMultiplier >= mModulo || mMultiplier < 1)
        return -1;
    if (mIncrement >= mModulo || mIncrement < 0)
        return -1;
    if (ggt(mMultiplier, mModulo) != 1)
        return -1;

    mSeed = ((mMultiplier * mSeed) + mIncrement) % mModulo;

    return mSeed;
}

int16_t IPrng::ggt(int16_t pVal1, int16_t pVal2)
{
    if (pVal1 == pVal2) return pVal1;
    if (pVal1 < pVal2)
    {
        int tmp = pVal2;
        pVal2 = pVal1;
        pVal1 = tmp;
    }
    while (1)
    {
        int rest = pVal2 % pVal1;
        if (rest == 0) return pVal1;
        pVal2 = pVal1;
        pVal1 = rest;
    }
}
